package com.zyx.redis.sentinel;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Yaxi.Zhang
 * @since 2022/6/27 19:44
 */
public class SentinelDemo {
    public static void main(String[] args) {
        // 连接池配置
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setMinIdle(5);
        // 哨兵信息
        Set<String> sentinels = new HashSet<>(Arrays.asList(
                "81.68.117.201:26380",
                "81.68.117.201:26381",
                "81.68.117.201:26382"
        ));
        // 创建连接池
        // masterName是我们配置给哨兵的服务名称
        // sentinels是哨兵信息
        // jedisPoolConfig是连接池配置
        // passsword是连接Redis服务器的密码
        String masterName = "mymaster";
        String passsword = "123321";
        JedisSentinelPool pool = new JedisSentinelPool(masterName, sentinels, jedisPoolConfig, passsword);
        // 获取客户端
        Jedis jedis = pool.getResource();
        // 执行两个命令
        // jedis.set("mykey", "myvalue");
        String myvalue = jedis.get("key4");
        // 打印信息
        System.out.println(myvalue);
    }
}
